package com.tedu.eguser.controller;

import com.tedu.eguser.mapper.UserMapper;
import com.tedu.eguser.pojo.dto.UserAddParam;
import com.tedu.eguser.pojo.dto.UserUpdateParam;
import com.tedu.eguser.pojo.entity.User;
import com.tedu.eguser.pojo.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/add")
    public String insert(UserAddParam addParam){
        User user = new User();
        BeanUtils.copyProperties(addParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int num = userMapper.insert(user);
        return num>0?"添加成功":"添加失败";
    }
    @GetMapping("list")
    public List<UserListVO> select(){
        List <UserListVO> userListVOS = userMapper.select();
        return userListVOS;
    }
    @PostMapping("/del")
    public String delete(Integer id){
        int num = userMapper.delete(id);
        return num>0?"删除成功":"删除失败";
    }
    @PostMapping("/update")
    public String update(UserUpdateParam userUpdateParam){
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam,user);
        user.setUpdateTime(new Date());
        int num = userMapper.update(user);
        return num>0?"修改成功":"修改失败";
    }

}
